/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Custommer;

import Bill.Bill;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import main.ketnoi;

/**
 *
 * @author ADMIN
 */
public class CustommerForBill extends javax.swing.JFrame {

    Connection con;
    DefaultTableModel model;

    /**
     * Creates new form CustommerForBill
     */
    public CustommerForBill() {
        initComponents();
        ketnoi a = new ketnoi();
        con = a.connect();
        model = (DefaultTableModel) jTable1.getModel();
        loadData();
        //code auto for CustomerCode
        jt1.setEnabled(false);
        int num1 = jTable1.getRowCount();
        jt1.setText("C" + (num1 + 1));
    }

    public void loadData() {
        try {
            PreparedStatement prst = con.prepareStatement("Select * from Customers");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                Vector v = new Vector();
                v.add(rs.getString(1));
                v.add(rs.getString(2));
                v.add(rs.getString(3));
                v.add(rs.getString(4));
                v.add(rs.getString(5));
                model.addRow(v);
            }
            jComboBox1.addItem("chose");
            PreparedStatement pre = con.prepareStatement("Select CustomerTypeName from CustomerTypes");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                jComboBox1.addItem(re.getString(1));
            }
            cbsearch.addItem("Choose");
            PreparedStatement st = con.prepareStatement("Select top 4 * from information_schema.columns where table_name = 'customers'");
            ResultSet rs1 = st.executeQuery();
            while (rs1.next()) {
                //lay du lieu cot thu 4: name,diem,lop
                String str1 = rs1.getString(4);
                cbsearch.addItem(str1);

            }

        } catch (SQLException ex) {
            Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reset() {
        jt2.setText("");
        jt3.setText("");
//        buttonGroup1.clearSelection();
        jRadioButton1.setSelected(true);
//        jRadioButton2.setSelected(false);
        jComboBox1.setSelectedIndex(0);
        //code auto for CustomerCode
        jt1.setEnabled(false);
        int num1 = jTable1.getRowCount();
        Calendar now = Calendar.getInstance();
        jt1.setText("C" + (num1 + 1));
    }

    public static boolean checkPhone(String n) {
        if (n == null || n.length() > 15 || n.length() < 7) {
            return false;
        } else {
            String strPattern = "[^0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return !m.find();
        }
    }

    public boolean checkCustomerCode() {
        try {
            if (jt1.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "CustomerCode invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
                jt1.setText("");
                jt1.requestFocus();
                return false;
            }
            PreparedStatement pre = con.prepareStatement("select CustomerCode from Customers");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                if (jt1.getText().equals(re.getString(1))) {
                    JOptionPane.showMessageDialog(this, "CustomerCode existed!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    //code auto for CustomerCode
                    jt1.setEnabled(false);
                    int num1 = jTable1.getRowCount();
                    Calendar now = Calendar.getInstance();
                    jt1.setText("C" + (num1 + 1));
                    return false;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public boolean checkCustomer() {
        if (jt2.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Name invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jt2.setText("");
            jt2.requestFocus();
            return false;
        } else if (jt3.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Phone invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jt3.setText("");
            jt3.requestFocus();
            return false;
        } else if (checkPhone(jt3.getText()) == false) {
            JOptionPane.showMessageDialog(this, "Phone invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jt3.setText("");
            jt3.requestFocus();
            return false;
        } else if (jComboBox1.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Type Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        txtsearch = new javax.swing.JTextField();
        search = new javax.swing.JButton();
        sort = new javax.swing.JButton();
        cbsearch = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jt1 = new javax.swing.JTextField();
        jt2 = new javax.swing.JTextField();
        jt3 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        update = new javax.swing.JButton();
        add = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        delete = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(231, 231, 231));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "CustommerLists", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CustomerCode", "CustomerName", "CustomerTypeCode", "RelationshipCode", "CustomerPhone"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        search.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/search2.png"))); // NOI18N
        search.setText("Search");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        sort.setText("Sort");
        sort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(sort, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(cbsearch, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(txtsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 18, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sort, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detail", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        jLabel1.setText("Code");

        jLabel2.setText("Name");

        jLabel3.setText("Phone");

        jt1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jt1ActionPerformed(evt);
            }
        });

        jt2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jt2ActionPerformed(evt);
            }
        });

        jt3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jt3ActionPerformed(evt);
            }
        });

        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Normal");

        jRadioButton2.setText("Discount");

        jLabel4.setText("Type");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jt1, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                    .addComponent(jt2)
                    .addComponent(jt3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jt1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                            .addComponent(jt2))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                            .addComponent(jt3, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
                        .addGap(26, 26, 26))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(43, 43, 43))))
        );

        update.setText("Update");
        update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateActionPerformed(evt);
            }
        });

        add.setText("Add");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        reset.setText("New");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        delete.setText("Delete");
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(update, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(delete, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(update, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delete, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 13, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 13, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 481, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int r = jTable1.getSelectedRow();
        jt1.setText(jTable1.getValueAt(r, 0).toString());
        jt2.setText(jTable1.getValueAt(r, 1).toString());
        jt3.setText(jTable1.getValueAt(r, 4).toString());
        //Normal - Discount
        if (jTable1.getValueAt(r, 3).equals("N")) {
            jRadioButton1.setSelected(true);
        } else {
            jRadioButton2.setSelected(true);
        }

        //Type
        if (jTable1.getValueAt(r, 2).equals("C")) {
            jComboBox1.setSelectedItem("Customer");
        } else if (jTable1.getValueAt(r, 2).equals("De")) {
            jComboBox1.setSelectedItem("Dealer");
        } else {
            jComboBox1.setSelectedItem("Distributor");
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
        if (cbsearch.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select search condition!");
        } else {
            try {
                // TODO add your handling code here:
                this.reset();
                int num = jTable1.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                //search
                String str1 = cbsearch.getSelectedItem().toString();
                String str = txtsearch.getText();
                PreparedStatement pst = con.prepareStatement("Select * from customers where " + str1 + " = ?");
                pst.setString(1, str);
                ResultSet rs1 = pst.executeQuery();
                Vector e = null;
                while (rs1.next()) {
                    e = new Vector();
                    e.add(rs1.getString(1));
                    e.add(rs1.getString(2));
                    e.add(rs1.getString(3));
                    e.add(rs1.getString(4));
                    e.add(rs1.getString(5));
                    model.addRow(e);
                }
            } catch (SQLException ex) {
                Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_searchActionPerformed

    private void sortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortActionPerformed
        if (cbsearch.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select sort condition!");
        } else {
            try {
                // TODO add your handling code here:
                this.reset();
                int num = jTable1.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                String str = cbsearch.getSelectedItem().toString();
                PreparedStatement pst = con.prepareStatement("Select * from customers order by " + str);

                ResultSet rs1 = pst.executeQuery();
                Vector e = null;
                while (rs1.next()) {
                    e = new Vector();
                    e.add(rs1.getString(1));
                    e.add(rs1.getString(2));
                    e.add(rs1.getString(3));
                    e.add(rs1.getString(4));
                    e.add(rs1.getString(5));
                    model.addRow(e);
                }
            } catch (SQLException ex) {
                Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_sortActionPerformed

    private void jt1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jt1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jt1ActionPerformed

    private void jt2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jt2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jt2ActionPerformed

    private void jt3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jt3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jt3ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        if (jComboBox1.getSelectedItem().equals("Dealer")) {
            jRadioButton2.setSelected(true);
            jRadioButton1.setEnabled(false);
            jRadioButton2.setEnabled(false);
        } else {
            jRadioButton1.setEnabled(true);
            jRadioButton2.setEnabled(true);
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateActionPerformed
        if (jt1.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please select Customer from table above");
        } else {
            if (checkCustomer() == true) {
                try {
                    // TODO add your handling code here:
                    String s = "";
                    if (jComboBox1.getSelectedItem().toString().equals("Dealer")) {
                        s = "De";
                    } else if (jComboBox1.getSelectedItem().toString().equals("Customer")) {
                        s = "C";
                    } else {
                        s = "Di";
                    }

                    String s2 = "";
                    if (jRadioButton1.isSelected() == true) {
                        s2 = "N";
                    } else {
                        s2 = "D";
                    }

                    PreparedStatement prst = con.prepareStatement("UPDATE Customers SET CustomerName=?,CustomerPhone=?,CustomerTypeCode=?,CustomerRelationshipCode=? WHERE CustomerCode=?");
                    prst.setString(1, jt2.getText());
                    prst.setString(2, jt3.getText());
                    prst.setString(3, s);
                    prst.setString(4, s2);
                    prst.setString(5, jt1.getText());
                    prst.executeUpdate();

                    //Load lai
                    int num = jTable1.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }
                    PreparedStatement prst1 = con.prepareStatement("Select * from Customers");
                    ResultSet rs1 = prst1.executeQuery();
                    while (rs1.next()) {
                        Vector b1 = new Vector();
                        b1.add(rs1.getString(1));
                        b1.add(rs1.getString(2));
                        b1.add(rs1.getString(3));
                        b1.add(rs1.getString(4));
                        b1.add(rs1.getString(5));
                        model.addRow(b1);
                    }

                    this.reset();
                } catch (SQLException ex) {
                    Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_updateActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed

        // TODO add your handling code here:
        //check
        if (checkCustomerCode() == true) {
            if (checkCustomer() == true) {
                try {
                    //add
                    String s = "";
                    if (jComboBox1.getSelectedItem().toString().equals("Dealer")) {
                        s = "De";
                    } else if (jComboBox1.getSelectedItem().toString().equals("Customer")) {
                        s = "C";
                    } else {
                        s = "Di";
                    }

                    String s2 = "";
                    if (jRadioButton1.isSelected() == true) {
                        s2 = "N";
                    } else {
                        s2 = "D";
                    }

                    PreparedStatement prst = con.prepareStatement("insert into Customers values (?,?,?,?,?)");
                    prst.setString(1, jt1.getText());
                    prst.setString(2, jt2.getText());
                    prst.setString(3, s);
                    prst.setString(4, s2);
                    prst.setString(5, jt3.getText());
                    prst.executeUpdate();

                    // Load lai co so du lieu
                    this.reset();
                    int num = jTable1.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }
                    PreparedStatement prst1 = con.prepareStatement("Select * from Customers");
                    ResultSet rs1 = prst1.executeQuery();
                    while (rs1.next()) {
                        Vector b1 = new Vector();
                        b1.add(rs1.getString(1));
                        b1.add(rs1.getString(2));
                        b1.add(rs1.getString(3));
                        b1.add(rs1.getString(4));
                        b1.add(rs1.getString(5));
                        model.addRow(b1);
                    }
                    //code auto for CustomerCode
                    jt1.setEnabled(false);
                    int num1 = jTable1.getRowCount();
                    Calendar now = Calendar.getInstance();
                    jt1.setText("C" + (num1 + 1));
                } catch (SQLException ex) {
                    Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_addActionPerformed

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
        // TODO add your handling code here:
        this.reset();
    }//GEN-LAST:event_resetActionPerformed

    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "     Are you sure? ", "---DELETE---", JOptionPane.YES_NO_OPTION, 2);
        if (n == 0) {
            try {
                //             TODO add your handling code here:
                PreparedStatement prst = con.prepareStatement("delete from Customers where CustomerCode=?");
                prst.setString(1, jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString());
                prst.executeUpdate();
                model.removeRow(jTable1.getSelectedRow());
                //            String str=null;
                //            PreparedStatement prst2 = con.prepareStatement("Delete from Suppliers where SupplierCode = ?");
                //            str = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
                //            prst2.setString(1, str);
                //            prst2.executeUpdate();
                //            model.removeRow(jTable1.getSelectedRow());
                this.reset();
            } catch (SQLException ex) {
                Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_deleteActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Bill b = new Bill();
        b.test(jt1.getText());
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CustommerForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CustommerForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustommerForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CustommerForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustommerForBill().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JComboBox cbsearch;
    private javax.swing.JButton delete;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jt1;
    private javax.swing.JTextField jt2;
    private javax.swing.JTextField jt3;
    private javax.swing.JButton reset;
    private javax.swing.JButton search;
    private javax.swing.JButton sort;
    private javax.swing.JTextField txtsearch;
    private javax.swing.JButton update;
    // End of variables declaration//GEN-END:variables
}
